Skip to content

Add access modifiers to generated imports#883

Open
arpit-garg-1995 wants to merge 5 commits into
apple:mainfrom
arpit-garg-1995:fix/777-import-access-modifiers
Open

Add access modifiers to generated imports#883
arpit-garg-1995 wants to merge 5 commits into
apple:mainfrom
arpit-garg-1995:fix/777-import-access-modifiers

Conversation

@arpit-garg-1995
Copy link
Copy Markdown

@arpit-garg-1995 arpit-garg-1995 commented Mar 20, 2026

Motivation

Starting in Swift 6, users can enable the InternalImportsByDefault upcoming feature flag, which makes all imports internal by default. When users generate code with an accessModifier of public or package, the generated declarations need to re-export the symbols they depend on. However, blindly applying the access modifier to every import causes a compiler error under -warnings-as-errors: HTTPTypes types appear only inside function bodies, never in public method signatures, so public import HTTPTypes is rejected by Swift 6.

Modifications

  • Add setsAccessModifier: Bool = true to ImportDescription; set to false for HTTPTypes in clientServerImports so the generator never emits public import HTTPTypes or package import HTTPTypes.
  • Update importDescriptions(adding:) in FileTranslator to respect the flag when propagating the configured access modifier.
  • Apply swift-format fixes to FileTranslator.swift, ServerTranslator.swift, and Test_TextBasedRenderer.swift.
  • Update Petstore reference snapshots to match new generator output: public import for OpenAPIRuntime and Foundation types; plain import for HTTPTypes.
  • Fix Examples/streaming-chatgpt-proxy/Sources/ChatGPT/Middleware.swift to use package import for Foundation and OpenAPIRuntime, matching the generator output for package-access targets.

Result

Format check passes. All unit tests pass with -Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error. The ChatGPT example builds correctly.

Test Plan

swift test -Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error — 319 tests, 0 failures, verified on macOS and Linux (Swift 6.2 Docker).

Copy link
Copy Markdown
Collaborator

@simonjbeaumont simonjbeaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to open the PR.

I'm sympathetic to the problem and open to a solution here. But I think this has been solved at the wrong layer.

The PR currently does this at the concrete implementation of a renderer and just unilaterally updates all imports to use a given access modifier.

This needs to be done in the types that conform to FileTranslator, e.g. see ClientFileTranslator, which is where the structured Swift representation is determined. You'll see there how access modifiers are used to generate the declarations in the file and where the import statements are defined.

This allows for two things, which we'll need before we can land this PR:

  • Other renderer implementations that may come in the future will get consistent behaviour because we removed the layering violation.
  • We can treat the imports used for the generated API differently from the additionalImports.

@arpit-garg-1995 arpit-garg-1995 force-pushed the fix/777-import-access-modifiers branch from d4b05f7 to 7d9b310 Compare April 27, 2026 14:58
@arpit-garg-1995
Copy link
Copy Markdown
Author

arpit-garg-1995 commented Apr 27, 2026

Support access modifiers on generated imports

Motivation

With the introduction of Swift 6.0's InternalImportsByDefault flag, generated declarations need a way to re-export the symbols they depend on. Supporting the configured access modifier on imports ensures they remain visible to consumers of the generated code when necessary.

Modifications

  • Add accessModifier property to ImportDescription.
  • Update TextBasedRenderer to conditionally render public and package access modifiers on imports.
  • Introduce importDescriptions(adding:) in FileTranslator to automatically propagate the configured access modifier to both built-in and additional imports.
  • Adopt the new import generation logic in ClientFileTranslator, ServerFileTranslator, and TypesFileTranslator.
  • Add unit tests in Test_TextBasedRenderer to verify the correct rendering of imports with different access modifiers, attributes, and OS conditions.

Result

Generated Swift files can now properly emit public import or package import statements when compiled with Swift 6.0 and above, based on the provided configuration.

Test Plan

Unit tests added in Test_TextBasedRenderer.
Testing with Act: Passed local medium act pull_request workflow

@simonjbeaumont
Copy link
Copy Markdown
Collaborator

@arpit-garg-1995 just a heads up that this package only supports Swift 6.1+ now so you don't need to do anything special in the generator to handle older compilers.

Support access modifiers on generated imports

### Motivation

With the introduction of Swift 6.0's `InternalImportsByDefault` flag, generated declarations need a way to re-export the symbols they depend on. Supporting the configured access modifier on imports ensures they remain visible to consumers of the generated code when necessary.

### Modifications

- Add `accessModifier` property to `ImportDescription`.
- Update `TextBasedRenderer` to conditionally render `public` and `package` access modifiers on imports, wrapped within an `#if compiler(>=6.0)` block to maintain compatibility with older compilers.
- Introduce `importDescriptions(adding:)` in `FileTranslator` to automatically propagate the configured access modifier to both built-in and additional imports.
- Adopt the new import generation logic in `ClientFileTranslator`, `ServerFileTranslator`, and `TypesFileTranslator`.
- Add unit tests in `Test_TextBasedRenderer` to verify the correct rendering of imports with different access modifiers, attributes, and OS conditions.

### Result

Generated Swift files can now properly emit `public import` or `package import` statements when compiled with Swift `6.0` and above, based on the provided configuration.

### Test Plan

Unit tests added in `Test_TextBasedRenderer`.
@arpit-garg-1995
Copy link
Copy Markdown
Author

Addressed the latest feedback in this commit.

Modifications

  • Removed the compiler-version conditional around the generated code path.
  • Dropped the special handling for older Swift compilers now that the package only supports Swift 6.1+.

Result

This simplifies the generator logic and aligns it with the package’s current Swift version support.

Test Plan

  • Ran swift test.
  • Testing with Act: Passed local medium act pull_request workflow

@swift-server-bot test this please

@simonjbeaumont simonjbeaumont added the 🆕 semver/minor Adds new public API. label May 5, 2026
@simonjbeaumont
Copy link
Copy Markdown
Collaborator

  • Ran swift test.
  • Testing with Act: Passed local medium act pull_request workflow

Are you sure you got the tests passing locally? They're failing for every Swift version and platform in CI.

@swift-server-bot test this please

Just FYI — this does nothing.

@arpit-garg-1995
Copy link
Copy Markdown
Author

Let me check and confirm. Will update soon.

…sAccessModifier flag to ImportDescription; update Petstore snapshots and ChatGPT example.
@arpit-garg-1995
Copy link
Copy Markdown
Author

Apologies for the earlier noise. The failures are now fixed:

  • Added setsAccessModifier: Bool = true to ImportDescription and set it to false for HTTPTypes, since its types only appear in function bodies and Swift 6 rejects public import for implementation-only imports under -warnings-as-errors.
  • Updated importDescriptions(adding:) to respect the flag, and refreshed the Petstore snapshots accordingly.
  • Fixed Examples/streaming-chatgpt-proxy/Sources/ChatGPT/Middleware.swift to use package import for Foundation and OpenAPIRuntime to match the generator output for package-access targets.

Verified locally: format check passes, 319 tests pass with -Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error on both macOS and Linux (Swift 6.2 Docker).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants